package com.pxmeta.bot.service.impl;

import com.github.plexpt.chatgpt.Chatbot;
import com.pxmeta.bot.service.IBotService;
import com.pxmeta.comment.entity.Comment;
import com.pxmeta.comment.entity.dto.CommentDTO;
import com.pxmeta.comment.service.ICommentService;
import com.pxmeta.dynamic.entity.Dynamic;
import com.pxmeta.dynamic.service.IDynamicService;
import com.pxmeta.user.entity.User;
import com.pxmeta.user.service.IUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
@RequiredArgsConstructor
@Slf4j
public class BotServiceImpl implements IBotService {

    public static final String BOT_USER_ID = "10000000000";
    public static final String BOT_SESSION_TOKEN = "eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2R0NNIn0..YNQ9qYBfLzoyxU7S.hiZ-tGLvMwjl1H8uXRSRM9JJ-9s1ThiUTq4YW3RCAJaewttb8Il23rr7bTH1QUEemMsG4SMn7Fse_AAC3bOGER77jnIEVCf9zcU1EXwUVdiysw_AXJxPvw-Oq-zjz2UHzHrlEWIPb5gv5-yXtu1WaTDMy1eOIWp63OGiD892fwxXVvWDry2MUUjCuv8kpoPSVvuwWzS8RuYFRX5DW2SbPcsGp8hjpGAUCixWYS-o3R9kVEcRHKNE1h2BpccvMIOyw_CgMAfSJzRIzkH1QVLQ3Akn_BrI3kdzeeErfaCXZ9MECA_vd2ZloxT2jSYndkqFOjz_LDXQy1bh28M9raH1S1xesKGYZBjQ8LR9t8LHvZXAf8sDGoKY5J0Z7Vmj_QJ_VSoaQyaV77z4zM_6C9TTBhBIcrglN_fhkVAjLg8TQXtEo23kEyAgNp4IdNAcKqXAZaoW982tpREMlU3ynKZxZ6IQy0tilGZscuPwV2vJSSYPipMSjnd4j1QOeFA97cJ5i7vjU8ewJ0ORJMEp05qsiZ0TcP1w5pdjzA6JPWyYtziOklR8CXi05u67y_iaD31iQKf_aDNnzVkiWWUCzeHAyLtZWQL-FVVb7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y_CP-XwZCnBgPUTxTmr-JVSW-ItsG43enl4AWyciR4CJwPm_daU3vQVt59kdLjo3et8tJi03y_R7DyOEm0nq9GdI4DP__mMM6oOmvFI3dlNHSF-QuhS7ELKO98yG0pNGeBTiEYhXHY62vygqYo0aESsxN-Z-6wh1tAmxrrrCexZkkZ5MhSIrkmNznVqfv9zSRne8lNiDm0Azp0ME-hVMLQJfkt9k00GfvDzNCPhApZI82AcXY_-9J6Q-lRXee0Hk5CBdRXEuQZbCWXil_c6Hw-2Ou88zfioKlMiEy8EzHACOVXNNsd5aNMkPEQd8xM-UswONKtB6InZgFhEIzPnQPlB0FAcMGTh4BYbdrGK--L5tWyCvMAO-DtblpOkjwjWvElvwDNQrtKx1ftq2aWI2hlNwOcaYxkIm56v2Vl0YVBmcpA9oBHBzq0w0UnyRRgzzCdOpZqUZ3L-Jxf3lyOfgoYKyuAMi7vUjq-VOg5GL1jxReidmAJhcKdVQZfdga3fL_db1xTTA9aYRJSR9Ws1h5rqxnemkEza3TwNjShDGcWNlxpdbvNsHmhtHe9VHRXgg3q7YsbVk9ZVrev5aNOOtK8qb-icWRvPEVLOZmgGDWpbgiWfXae4fj2CYctkCbg0tsxedrfXLLyQvAY625r7QFfj65GeJfjtpBpegdRpCy_Q.F0_-GXDQGqiZygpBpiltOw";
    private final IUserService userService;
    private final IDynamicService dynamicService;
    private final ICommentService commentService;




    @Override
    public void replyDynamic(Long dynamicId) {
        Chatbot chatbot = new Chatbot(BOT_SESSION_TOKEN);
        try {
            if (null == dynamicId){
                return;
            }
            Dynamic dynamic = dynamicService.getById(dynamicId);
            Map<String, Object> chatResponse = chatbot.getChatResponse(dynamic.getContent());
            Object message = chatResponse.get("message");
            User botUser = userService.getByUsername(BOT_USER_ID);
            CommentDTO dto = new CommentDTO();
            dto.setDynamicId(dynamicId);
            dto.setContent(message.toString());
            dto.setParentId(0L);
            dto.setReplyUserId(dynamic.getUserId());
            commentService.comment(dto, botUser.getId());
        }catch (Exception e){
            log.error("机器人回复失败，错误信息：{}",e.getMessage());
        }
    }

    @Override
    public void replyComment(Long commentId) {
        Chatbot chatbot = new Chatbot(BOT_SESSION_TOKEN);
        log.info("开始回复...commentId:{}",commentId);
        if (commentId == null){
            return;
        }
        User botUser = userService.getByUsername(BOT_USER_ID);
        Comment comment = commentService.getById(commentId);
        //防止机器人自己回复自己。
        if (comment.getUserId().equals(1L)){
            log.info("不能评论自己");
            return;
        }
        String content = comment.getContent();
        Map<String, Object> chatResponse = chatbot.getChatResponse(content);
        Object message = chatResponse.get("message");
        CommentDTO dto = new CommentDTO();
        dto.setDynamicId(comment.getDynamicId());
        dto.setReplyUserId(comment.getUserId());
        dto.setParentId(comment.getParentId());
        dto.setContent(message.toString());
        commentService.comment(dto, botUser.getId());
    }
}
